﻿Imports System.Data.OleDb

Public Class addProperty
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub buttonaddprop_Click(sender As Object, e As EventArgs) Handles buttonaddprop.Click

        Dim Image1GUID As New System.Guid
        Image1GUID = Guid.NewGuid()
        Dim NewImageFileName1 = Image1GUID.ToString() + ".jpg"

        Dim Image2GUID As New System.Guid
        Image2GUID = Guid.NewGuid()
        Dim NewImageFileName2 = Image2GUID.ToString() + ".jpg"

        Dim Image3GUID As New System.Guid
        Image3GUID = Guid.NewGuid()
        Dim NewImageFileName3 = Image3GUID.ToString() + ".jpg"

        Dim Image4GUID As New System.Guid
        Image4GUID = Guid.NewGuid()
        Dim NewImageFileName4 = Image4GUID.ToString() + ".jpg"

        If image1.HasFile Then
            Dim picLocation = Request.MapPath("imageUploads") & "/" & NewImageFileName1
            image1.SaveAs(picLocation)
        End If

        If image2.HasFile Then
            Dim picLocation2 = Request.MapPath("imageUploads") & "/" & NewImageFileName2
            image2.SaveAs(picLocation2)
        End If

        If image3.HasFile Then
            Dim picLocation3 = Request.MapPath("imageUploads") & "/" & NewImageFileName3
            image3.SaveAs(picLocation3)
        End If

        If image4.HasFile Then
            Dim picLocation4 = Request.MapPath("imageUploads") & "/" & NewImageFileName4
            image4.SaveAs(picLocation4)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
        Dim SqlString As String = "Insert into Properties(property_name, property_rate, property_bedrooms, property_bathrooms, property_square_ft, property_type, property_location, property_car_garage, property_description, property_year, property_image_1, property_image_2, property_image_3, property_image_4) Values (@f1, @f2, @f3, @f4, @f5, @f6, @f7, @f8, @f9, @f10, @f11, @f12, @f13, @f14)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", property_name.Text)
        cmd.Parameters.AddWithValue("@f2", property_rate.Text)
        cmd.Parameters.AddWithValue("@f3", propertybedrooms.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", propertybathrooms.SelectedValue)
        cmd.Parameters.AddWithValue("@f5", property_sqft.Text)
        cmd.Parameters.AddWithValue("@f6", proptype.SelectedValue)
        cmd.Parameters.AddWithValue("@f7", proploc.SelectedValue)
        cmd.Parameters.AddWithValue("@f8", cargarage.SelectedValue)
        cmd.Parameters.AddWithValue("@f9", property_description.Text)
        cmd.Parameters.AddWithValue("@f10", property_year.Text)
        cmd.Parameters.AddWithValue("@f11", "imageUploads/" & NewImageFileName1)
        cmd.Parameters.AddWithValue("@f12", "imageUploads/" & NewImageFileName2)
        cmd.Parameters.AddWithValue("@f13", "imageUploads/" & NewImageFileName3)
        cmd.Parameters.AddWithValue("@f14", "imageUploads/" & NewImageFileName4)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Get Agent Id
        Dim oleDbConn3 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
        Dim SqlString3 As String = "SELECT * FROM AGENTS WHERE agentUsername=@f1"
        Dim cmd3 As OleDbCommand = New OleDbCommand(SqlString3, oleDbConn3)
        cmd3.CommandType = CommandType.Text
        cmd3.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbConn3.Open()
        Dim agentData As OleDbDataReader = cmd3.ExecuteReader()
        agentData.Read()
        Dim agentId As String = agentData("ID")

        'Get Property ID
        Dim oleDbConn4 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
        Dim SqlString4 As String = "SELECT TOP 1 ID FROM Properties ORDER BY ID DESC"
        Dim cmd4 As OleDbCommand = New OleDbCommand(SqlString4, oleDbConn4)
        cmd4.CommandType = CommandType.Text
        oleDbConn4.Open()
        Dim lastRecord As OleDbDataReader = cmd4.ExecuteReader()
        lastRecord.Read()
        Dim record As String = lastRecord("ID")
        record = record + 1

        Dim oleDbConn2 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
        Dim SqlString2 As String = "Insert into PropertyAgentJunction(Property_FKID, Agent_FKID) Values (@f1, @f2)"
        Dim cmd2 As OleDbCommand = New OleDbCommand(SqlString2, oleDbConn2)
        cmd2.CommandType = CommandType.Text
        cmd2.Parameters.AddWithValue("@f1", record)
        cmd2.Parameters.AddWithValue("@f2", agentId)
        oleDbConn2.Open()
        cmd2.ExecuteNonQuery()

        Dim oleDbConn5 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
        Dim SqlString5 As String = "Insert into PropertyTagJunction(Property_FKID, Tag_FKID) Values (@f1, @f2)"
        oleDbConn5.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                Dim cmd5 As OleDbCommand = New OleDbCommand(SqlString5, oleDbConn5)
                cmd5.CommandType = CommandType.Text
                cmd5.Parameters.AddWithValue("@f1", record)
                cmd5.Parameters.AddWithValue("@f2", cb.Value)
                cmd5.ExecuteNonQuery()
            End If
        Next



        Response.Redirect("default.aspx")


    End Sub
End Class